#include<pthread.h>
#include<iostream>
#include<unistd.h>

void *routine(void *args)
{
    std::string name = (const char *)args;
    while (true)
    {
        sleep(1);
        std::cout << "我是新线程: name: " << name << ", pid: " << getpid() << std::endl;
        pthread_cancel(pthread_self());
        // int a = 10;
        // a /= 0;
    }
    return nullptr;
}

int main()
{
    pthread_t tid;
    int n = pthread_create(&tid, nullptr, routine, (void *)"thread-l");
    if (n < 0)
        std::cerr << "创建线程失败：" << std::endl;
    while (true)
    {
        std::cout << "我是主线程..." << ", pid: " << getpid() << std::endl;
        sleep(1);
    }
    return 0;
}